/*
 * @Date: 2020-08-25 17:49:41
 * @LastEditors: xujianxing
 * @LastEditTime: 2020-08-26 14:36:01
 * @FilePath: \nili\src\common\interceptor\transform.interceptor.ts
 */
import {
  Injectable,
  NestInterceptor,
  ExecutionContext,
  CallHandler,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

export interface Response<T> {
  data: T;
}

@Injectable()
export class TransformInterceptor<T>
  implements NestInterceptor<T, Response<T>> {
  intercept(
    context: ExecutionContext,
    next: CallHandler,
  ): Observable<Response<T>> {
    // 此流包含从路由程序返回的值
    return next.handle().pipe(map(data => ({ data })));
  }
}
